package com.graduation.fq.student.Process;

import com.graduation.fq.student.Utils.Const;
import com.graduation.fq.student.Utils.ChooseUrlIndex;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jifang
 * Time 2016/5/27 15:59.
 */
public class ChooseProcess extends BaseProcess {

    private List<String> urls = new ArrayList<>();

    {
        urls.add("/choose/is_added.do?u_id=%s&c_id=%s");
        urls.add("/choose/add.do?u_id=%s&c_id=%s");
        urls.add("/choose/cancel.do?u_id=%s&c_id=%s");
    }

    private ChooseUrlIndex index;
    private int uId;
    private int cId;

    private boolean is_added;

    public ChooseProcess(ChooseUrlIndex index, int uId, int cId) {
        this.index = index;
        this.uId = uId;
        this.cId = cId;
    }

    public boolean is_added() {
        return is_added;
    }

    public void setIs_added(boolean is_added) {
        this.is_added = is_added;
    }

    @Override
    String getRequestUrl() {
        return String.format(urls.get(index.getIndex()), uId, cId);
    }

    @Override
    String getParameter() {
        return null;
    }

    @Override
    void onResult(JSONObject object) {
        int status = object.optInt("status");
        setStatus(status);
        if (status == Const.HTTP_SUCCESS && index == ChooseUrlIndex.IS_ADDED) {
            setIs_added(object.optBoolean("data"));
        }
    }
}
